﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web.UI;
using System.IO;

namespace MyPhotoIndex.Plugins.WebHost.VirtualPages
{
    public abstract class VirtualPage
    {
        private static Dictionary<string, VirtualPage> m_registeredPages = new Dictionary<string, VirtualPage>();
        protected string ContentType = "text/xml";

        static VirtualPage()
        {
            m_registeredPages.Add("LoginPage.mvp", new LoginPage());
            m_registeredPages.Add("RSSFeed.mvp", new RSSFeed());
        }

        public abstract VirtualPage Clone();

        public static void ProcessPage(HttpListenerRequest request, HttpListenerResponse response, string fileName)
        {
            VirtualPage page = m_registeredPages[fileName].Clone();
            page.ContentType = Utils.GetContentTypeFromFileExtention(".html");
            page.Load(request, response, fileName);

            page.Response.ContentType = page.ContentType;
            response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate");

     //      page.Response.ContentLength64 = fs.Length;

            using (Stream output =  page.Response.OutputStream)
            using (TextWriter textWriter = new StreamWriter(output))
            using (HtmlTextWriter htmlWriter = new HtmlTextWriter(textWriter))
            {
                page.Render(htmlWriter);
            }
        }

        private HttpListenerRequest m_request;

        protected HttpListenerRequest Request
        {
            get { return m_request; }
        }
        private HttpListenerResponse m_response;

        protected HttpListenerResponse Response
        {
            get { return m_response; }
        }
        private string m_fileName;

        protected string FileName
        {
            get { return m_fileName; }
        }
        private Dictionary<string, string> m_formVars = new Dictionary<string, string>();

        protected Dictionary<string, string> FormVars
        {
            get { return m_formVars; }
        }

        public void Load(HttpListenerRequest request, HttpListenerResponse response, string fileName)
        {
            m_request = request;
            m_response = response;
            m_fileName = fileName;

            using (Stream inputStream = m_request.InputStream)
            using (StreamReader reader = new StreamReader(inputStream))
            {
                string requestContent = reader.ReadToEnd();
                if (string.IsNullOrEmpty(requestContent) == false)
                {
                    string[] formParameters = requestContent.Split(new char[] { '&' });

                    foreach (string formParam in formParameters)
                    {
                        string[] pair = formParam.Split(new char[] { '=' });
                        m_formVars.Add(pair[0], pair[1]);
                    }
                }
            }

            LoadPage();
        }

        public virtual void Render(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Html);

            writer.RenderBeginTag(HtmlTextWriterTag.Head);
            writer.RenderBeginTag(HtmlTextWriterTag.Title);
            // page title
            writer.Write(FileName);
            // page title
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Body);

            RenderPage(writer);

            writer.RenderEndTag();

            writer.RenderEndTag();
        }

        public abstract void LoadPage();
        public abstract void RenderPage(HtmlTextWriter writer);
    }
}